﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    /// <summary>
    /// Summary description for DAStation
    /// </summary>
    public class DAStation : DbObject
    {
        protected SqlConnection connec;
        public DAStation(SqlConnection Connection)
        {
            connec = Connection;
            //
            // TODO: Add constructor logic here
            //
        }

        public DataSet load_danh_sach_station(int ip_station_id)
        {
            // create the parameter
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@Id", SqlDbType.Int,4)
										 };
            // set the 0values
            parameters[0].Value = ip_station_id;

            DataSet ds_dm_station = RunProcedure("sp_TB_Station_LoadStation",parameters, "TB_Station", connec);

            return ds_dm_station;
        }
        public DataSet load_danh_sach_station_chua_dung()
        {
            DataSet ds_dm_station = RunProcedure("sp_TB_Station_LoadStationChuaDung", "TB_Station", connec);

            return ds_dm_station;
        }
        public bool is_station_exist(string ip_station_code, int ip_i_id)
        {
            // create the parameter
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@StationCode", SqlDbType.VarChar,10),
                                             new SqlParameter("@Id", SqlDbType.Int,4)
										 };
            // set the 0values
            parameters[0].Value = ip_station_code;
            parameters[1].Value = ip_i_id;

            DataSet ds_dm_station = RunProcedure("sp_TB_Station_CheckExistStation", parameters, "TB_Station", connec);

            if (ds_dm_station.Tables[0].Rows.Count > 0) return true;

            return false;
        }
        public bool is_station_available(string ip_station_code)
        {
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@StationCode", SqlDbType.VarChar,10)
										 };
            // set the 0values
            parameters[0].Value = ip_station_code;

            DataSet ds_dm_station = RunProcedure("sp_TB_Station_CheckStationAvailable", parameters, "TB_Station", connec);

            if (ds_dm_station.Tables[0].Rows.Count > 0) return false;  // ko dùng đc, có thằng dùng rồi

            return true;
        }

        public int Update(string ip_str_station_code, int ip_i_id)
        {
            int v_i_result = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@StationCode", SqlDbType.VarChar,10) ,
                                             new SqlParameter("@Id", SqlDbType.Int,4)
										 };
            // set the 0values
            parameters[0].Value = ip_str_station_code;
            parameters[1].Value = ip_i_id;
            // create the parameter
            RunProcedure("sp_TB_Station_Update", parameters, out v_i_result, connec);
            return v_i_result;
        }
        public int Insert(string ip_str_station_code)
        {
            int v_i_result = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@StationCode", SqlDbType.VarChar,10)
										 };
            // set the 0values
            parameters[0].Value = ip_str_station_code;
            RunProcedure("sp_TB_Station_Insert", parameters, out v_i_result, connec);
            return v_i_result;
        }
        public int Delete(int ip_i_id)
        {
            int v_i_result = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@Id", SqlDbType.Int,4)
										 };
            // set the 0values
            parameters[0].Value = ip_i_id;
            RunProcedure("sp_TB_Station_Delete", parameters, out v_i_result, connec);
            return v_i_result;
        }
    }
}